Simple Hangman bằng Python
1.423 lượt xem;
- hangman.py
- project /
1 # This is Hangman game. Computer randomly picks a word, and player needs to
2 # guess what is the word. The player also has to guess the word before
3 # hangman is completely drawn.
4 # Check also flowchart.pdf for more info.
5
6 from random import randint
7 from hangs import HANGMAN_PICS # Imports list of Hangman drawings
8
9 def main():
10 gameEnd = False
11
12 while gameEnd == False:
13 playGame()
14
15 # Ask if user would like to play again
16 while True:
17 decision = input('Would you like to play again? (yes or no): ').lower()
18 if decision == 'yes':
19 break
20 if decision == 'no':
21 gameEnd = True
22 break
23
24 def playGame():
25 ''' Plays one Hangman game '''
26 # Initial values
27 secretWord = pick_word()
28 guessedLetters = ['_'] * len(secretWord)
29 wrongLetters = ''
30
31 print('\n' * 2 + 'H A N G M A N')
32
33 while True:
34 print('\n' + 'Secret word: ' + ' '.join(guessedLetters).capitalize())
35 print('Wrong letters: ' + ' '.join(wrongLetters))
36 print(HANGMAN_PICS[len(wrongLetters)])
37
38 # Check winning or losing status
39 if ''.join(guessedLetters).isalpha():
40 print('You won! :)')
41 break
42 if len(wrongLetters) >= len(HANGMAN_PICS) - 1:
43 print('You lost! :(. It was {}'.format(secretWord.title()))
44 break
45
46 inputLetter = getLetter()
47
48 # Check if user guessed the letter
49 if inputLetter in secretWord:
50 for charNum in range(len(secretWord)):
51 if inputLetter == secretWord[charNum]:
52 # Add letter to guessed letters
53 guessedLetters[charNum] = inputLetter
54 # Check if user already typed this letter
55 elif inputLetter in wrongLetters:
56 print('You have already typed this letter!')
57 # Else add to wrong letters
58 else:
59 wrongLetters = wrongLetters + inputLetter
60
61 def getLetter():
62 '''
63 Gets letter from user as input and validates if the input
64 is actually a letter.
65 '''
66
67 while True:
68 letter = input('Guess a letter: ')
69 if len(letter) == 1 and letter.isalpha() == True:
70 return letter.lower()
71
72
73 def pick_word():
74 '''
75 Creates a list of words with minimum three letters from file.
76 Picks and returns random word from the list.
77 '''
78
79 filePath = 'words.txt'
80 file = open(filePath, 'r')
81 words = []
82
83 # Add words with min three characters
84 for line in file:
85 line = line.rstrip('\n')
86 if len(line) >= 3:
87 words.append(line.lower())
88
89 file.close()
90
91 # Draw random word from the list
92 word = words[randint(0, len(words))]
93 return word
94
95 if __name__ == '__main__':
96 main()
2 # guess what is the word. The player also has to guess the word before
3 # hangman is completely drawn.
4 # Check also flowchart.pdf for more info.
5
6 from random import randint
7 from hangs import HANGMAN_PICS # Imports list of Hangman drawings
8
9 def main():
10 gameEnd = False
11
12 while gameEnd == False:
13 playGame()
14
15 # Ask if user would like to play again
16 while True:
17 decision = input('Would you like to play again? (yes or no): ').lower()
18 if decision == 'yes':
19 break
20 if decision == 'no':
21 gameEnd = True
22 break
23
24 def playGame():
25 ''' Plays one Hangman game '''
26 # Initial values
27 secretWord = pick_word()
28 guessedLetters = ['_'] * len(secretWord)
29 wrongLetters = ''
30
31 print('\n' * 2 + 'H A N G M A N')
32
33 while True:
34 print('\n' + 'Secret word: ' + ' '.join(guessedLetters).capitalize())
35 print('Wrong letters: ' + ' '.join(wrongLetters))
36 print(HANGMAN_PICS[len(wrongLetters)])
37
38 # Check winning or losing status
39 if ''.join(guessedLetters).isalpha():
40 print('You won! :)')
41 break
42 if len(wrongLetters) >= len(HANGMAN_PICS) - 1:
43 print('You lost! :(. It was {}'.format(secretWord.title()))
44 break
45
46 inputLetter = getLetter()
47
48 # Check if user guessed the letter
49 if inputLetter in secretWord:
50 for charNum in range(len(secretWord)):
51 if inputLetter == secretWord[charNum]:
52 # Add letter to guessed letters
53 guessedLetters[charNum] = inputLetter
54 # Check if user already typed this letter
55 elif inputLetter in wrongLetters:
56 print('You have already typed this letter!')
57 # Else add to wrong letters
58 else:
59 wrongLetters = wrongLetters + inputLetter
60
61 def getLetter():
62 '''
63 Gets letter from user as input and validates if the input
64 is actually a letter.
65 '''
66
67 while True:
68 letter = input('Guess a letter: ')
69 if len(letter) == 1 and letter.isalpha() == True:
70 return letter.lower()
71
72
73 def pick_word():
74 '''
75 Creates a list of words with minimum three letters from file.
76 Picks and returns random word from the list.
77 '''
78
79 filePath = 'words.txt'
80 file = open(filePath, 'r')
81 words = []
82
83 # Add words with min three characters
84 for line in file:
85 line = line.rstrip('\n')
86 if len(line) >= 3:
87 words.append(line.lower())
88
89 file.close()
90
91 # Draw random word from the list
92 word = words[randint(0, len(words))]
93 return word
94
95 if __name__ == '__main__':
96 main()